﻿using System;
using System.Data;

namespace wuliuback
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            string userName = Request["user_name"];
            string name = Request["name"];
            string password = Request["password"];
            string mobile = Request["mobile"];
            string identityCard = Request["identity_card"];
            string userType = Request["user_type"];
            string license_plate = Request["license_plate"];
            string driving_license = Request["driving_license"];
            string vehicle_license = Request["vehicle_license"];
            string email = Request["email"];
            string address = Request["address"];
            string zip = Request["zip"];
            string company_name = Request["company_name"];

            string mdtPw = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower().Substring(8, 16);

            string code = "";
            string msg = "";
            string user = "";

            if (mobile == null || userName == null || mobile.Length <= 0 || userName.Length <= 0)
            {
                code = "-2";
                msg = "用户名或密码为空";
                Response.Output.WriteLine("{\"code\":\"" + code + "\",\"msg\":\"" + msg  + "\"}");
                return;
            }
            String sql = "";
            sql = "select * from t_user where mobile='" + mobile + "' or user_id='" + userName + "'";
            
            DbHelper dbHelper = new DbHelper();
            DataSet dataSet = dbHelper.Select(sql);
            DataTable table = dataSet.Tables[0];
            if (table.Rows.Count <= 0)
            {
                sql = "insert into t_user(user_id,name,password,mobile,identity_card,register_date,"
                    + "user_type,license_plate,driving_license,vehicle_license,email,address,zip,company_name)"
                    + "values('" + userName + "','" + name + "','" + mdtPw + "','" + mobile + "','" +
                              identityCard + "','" + DateTime.Now.ToString() + "','" + userType + "','" + license_plate + "','" + driving_license
                              + "','" + vehicle_license + "','" + email + "','" + address + "','" + zip + "','" + company_name + "')";
                dbHelper.Insert(sql);

                sql = "select * from t_user where (mobile='" + mobile + "' or user_id='"+userName+"') and password='" + mdtPw + "'";
                DataSet dataSet1 = dbHelper.Select(sql);
                DataTable table1 = dataSet1.Tables[0];
                if (table1.Rows.Count > 0)
                {
                    code = "0";
                    user = JsonUtil.DataTableToJson(table1);
                    Response.Output.WriteLine("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"user\":" + user + "}");
                }
                else
                {
                    code = "-1";
                    Response.Output.WriteLine("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\"}");
                }
            }
            else
            {
                code = "-1";
                msg = "已存在该用户名或电话";
                Response.Output.WriteLine("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\"}");
            }

            
        }

    }
}
